# -*- coding: utf-8 -*-
"""
Created on Mon Nov 13 21:31:39 2017

@author: zf
"""

from selenium import webdriver
import time,xlrd,xlwt,re

class shua():
    def __init__(self,ID,psd,test_choose,LESSON,number,url,sleeptime=0,question=3):
        self.ID=ID
        self.psd=psd
        
        self.num=0
        self.LESSON=str(LESSON)
        self.url=url
        self.sleeptime=int(sleeptime)
        self.number=int(number)
        self.URL=test_choose
        self.work=xlwt.Workbook()
        self.sheet =self.work.add_sheet("试题库测试")
        self.style = xlwt.easyxf('font: bold 1, color red;')
        self.sheet.row(0).height = 256 *3
        self.sheet.col(0).width = 256 *100
        self.sheet.col(1).width = 256 *10
        self.sheet.col(2).width = 256 *20
        self.sheet.write(0,0,"题目",self.style)                      
        self.sheet.write(0,0+1,"答案",self.style)
        self.sheet.write(0,0+2,"题号",self.style)         
        self.weizhi="试题库测试.xls"
        self.data=xlrd.open_workbook('exercise.xls')
        self.table1=self.data.sheets()[0]
        self.col1= self.table1.col_values(0)
        self.col17= self.table1.col_values(7)
        
        self.box1=[]
        self.box17=[]
        for i1 in self.col1:
                self.box1.append(i1.replace(' ',''))
        for i17 in self.col17:
                self.box17.append(i17)
                
        if int(question)==20:#当为单选题+多选题时
            self.table3=self.data.sheets()[1]
            self.col3=self.table3.col_values(0)
            self.col32= self.table3.col_values(2)
            
            self.box3=[]
            self.box32=[]
            for i3 in self.col3:
                self.box3.append(i3.replace(' ',''))
            for i32 in self.col32:
                self.box32.append(i32)
        if int(question)==21:#当为单选题+判断题时
            
            self.table2=self.data.sheets()[1]
            self.col2= self.table2.col_values(0)
            self.col27= self.table2.col_values(7)
            self.box2=[]
            self.box27=[]
            for i2 in self.col2:
                self.box2.append(i2.replace(' ',''))
            for i27 in self.col27:
                self.box27.append(i27)
        if int(question)==3:
            
            self.table2=self.data.sheets()[1]
            self.col2= self.table2.col_values(0)
            self.col27= self.table2.col_values(7)
            self.box2=[]
            self.box27=[]
            for i2 in self.col2:
                self.box2.append(i2.replace(' ',''))
            for i27 in self.col27:
                self.box27.append(i27)
                
            self.table3=self.data.sheets()[2]
            self.col3=self.table3.col_values(0)
            self.col32= self.table3.col_values(2)
            
            self.box3=[]
            self.box32=[]
            for i3 in self.col3:
                self.box3.append(i3.replace(' ',''))
            for i32 in self.col32:
                self.box32.append(i32)
    def login(self):
        #self.browser= webdriver.Chrome(executable_path='\chromedriver\chromedriver.exe')
        self.browser= webdriver.Firefox()
        self.browser.get(self.url)
        self.browser.find_element_by_id("logname").send_keys(self.ID)
        self.browser.find_element_by_id("password").send_keys(self.psd)
        
    def test(self):
        time.sleep(1)
        self.browser.find_element_by_id("loginBtn").click()
        time.sleep(1)
        self.browser.find_element_by_css_selector('#surveyCourseDiv > div.listcourse > ul:nth-child('+str(self.LESSON)+') > li.courseImg').click()#张浩
        time.sleep(1)
        self.browser.find_element_by_css_selector('#right > div.courseDiv > div.lf > a > span').click()
        time.sleep(1)
        text=self.browser.page_source
        find=re.compile(r'<a href="(.*?)"').findall(text)
        self.URL1='http://tkkc.hfut.edu.cn/'+find[5].replace('amp;','')
        self.URL2='http://tkkc.hfut.edu.cn/'+find[6].replace('amp;','')
        self.URL3='http://tkkc.hfut.edu.cn/'+find[7].replace('amp;','')
        self.URL_TEST='http://tkkc.hfut.edu.cn/'+find[8].replace('amp;','')
        if int(self.URL)==1:
            self.browser.get(self.URL1)
            time.sleep(5)
            self.find()
            self.browser.get(self.URL2)
            time.sleep(5)
            self.find()
            self.browser.get(self.URL3)
            time.sleep(5)
            self.find()
        elif int(self.URL)==8:
            self.browser.get(self.URL_TEST)
            time.sleep(5)
            self.find()
        
    def find(self):
        for i in range(self.number):
            time.sleep(self.sleeptime)
            title=self.browser.find_element_by_xpath('//*[@id="Exercise_Type"]').text
            if str(title)=='单选题':
                tex=str(self.browser.find_element_by_xpath('//*[@id="D_title"]').text)
                text=tex.replace(' ','')                                 
                n=-1   
                self.sheet.write(self.num+1,0,tex)                               
                for a in self.box1:
                    n+=1
                    if a == text:
                        self.sheet.write(self.num+1,1,self.box17[n]) 
                        self.sheet.write(self.num+1,2,'第 '+str(self.num+1)+'题')
                        if str(self.box17[n])=='A':
                           self.browser.find_element_by_xpath('//*[@id="D_A"]').click() 
                        elif str(self.box17[n])=='B':
                            self.browser.find_element_by_xpath('//*[@id="D_B"]').click()
                        elif str(self.box17[n])=='C':
                            self.browser.find_element_by_xpath('//*[@id="D_C"]').click()
                        elif str(self.box17[n])=='D':
                            self.browser.find_element_by_xpath('//*[@id="D_D"]').click()
                        break
    
            elif str(title)=='多选题':
                k=-1
                tex=str(self.browser.find_element_by_xpath('//*[@id="Duo_title"]').text)
                text=tex.replace(' ','') 
                self.sheet.write(self.num+1,0,tex)                                 
                for b in self.box2:
                    k+=1
                    if b == text:
                        self.sheet.write(self.num+1,1,self.box27[k])
                        self.sheet.write(self.num+1,2,'第 '+str(self.num+1)+'题')
                        if 'A'in str(self.box27[k]):
                           self.browser.find_element_by_xpath('//*[@id="Duo_A"]').click() 
                        if 'B'in str(self.box27[k]):
                            self.browser.find_element_by_xpath('//*[@id="Duo_B"]').click()
                        if 'C'in str(self.box27[k]):
                            self.browser.find_element_by_xpath('//*[@id="Duo_C"]').click()
                        if 'D'in str(self.box27[k]):
                            self.browser.find_element_by_xpath('//*[@id="Duo_D"]').click()
                        if 'E'in str(self.box27[k]):
                            self.browser.find_element_by_xpath('//*[@id="Duo_E"]').click()
                            
                        break
                    
            elif str(title)=='判断题':
                m=-1
                tex=str(self.browser.find_element_by_xpath('//*[@id="P_title"]').text)
                text=tex.replace(' ','')
                self.sheet.write(self.num+1,0,tex) 
                for c in self.box3:
                    m+=1
                    if c == text:
                        self.sheet.write(self.num+1,1,self.box32[m]) 
                        self.sheet.write(self.num+1,2,'第 '+str(self.num+1)+'题')
                        if str(self.box32[m])=='正确':
                           self.browser.find_element_by_xpath('//*[@id="P_A"]').click() 
                        elif str(self.box32[m])=='错误':
                            self.browser.find_element_by_xpath('//*[@id="P_B"]').click()
                        break
                    
                    
                   
            self.num+=1
            self.browser.find_element_by_xpath('//*[@id="submitButton_id"][2]').click()     
        self.work.save(self.weizhi)
        
        


        
        


